package com.inspur.edp.common;

import com.google.common.collect.Maps;
import io.iec.edp.caf.boot.context.CAFContext;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.api.service.RpcClient;
import io.iec.edp.caf.rpc.api.support.Type;
import io.iec.edp.caf.sysmanager.api.data.user.User;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class CommonUtils {
    private static Boolean paas = null;
    public static boolean getPaas() {
        /*
        PaaSConfigProps paaSConfigProps = SpringBeanUtils.getBean(PaaSConfigProps.class);
        if (paaSConfigProps == null) { return false; }
        Boolean enabled = paaSConfigProps.getEnabled();
        if (enabled == null || !enabled) {
            return false;
        }
        */
        return true;
    }
    public static User findUserInfo() {
        // 获取当前用户id
        String userId = CAFContext.current.getUserId();
        if (StringUtils.isEmpty(userId)) {
            throw new RuntimeException("Can not get userId.");
        }
        RpcClient client = SpringBeanUtils.getBean(RpcClient.class);
        LinkedHashMap<String, Object> params = new LinkedHashMap<>();
        params.put("userId", userId);
        User user = client.invoke(User.class, "io.iec.edp.caf.sysmanager.api.manager.user.UserManager.getUserBasicInfo", "Sys", params, null);
        if (Objects.isNull(user)) {
            throw new RuntimeException("can not get User info : " + userId);
        }

        return user;
    }

    public static User findUserInfo(String userId) {
        // 获取当前用户id
        RpcClient client = SpringBeanUtils.getBean(RpcClient.class);
        LinkedHashMap<String, Object> params = new LinkedHashMap<>();
        params.put("userId", userId);
        User user = client.invoke(User.class, "io.iec.edp.caf.sysmanager.api.manager.user.UserManager.getUserBasicInfo", "Sys", params, null);
        if (Objects.isNull(user)) {
            return null;
        }

        return user;
    }

    /**
     * 根据用户id批量查询用户信息
     *
     * @param idList
     * @return
     */
    public static Map<String, User> getUsersByIds(List<String> idList) {
        Map<String, User> userId2UserMap = Maps.newHashMap();
        RpcClient client = SpringBeanUtils.getBean(RpcClient.class);
        LinkedHashMap<String, Object> params = new LinkedHashMap<>();
        params.put("list", idList);
        params.put("field", "id");
        params.put("mode", null);
        Type<List> type = new Type<>(List.class, User.class);
        List<User> userList = client.invoke(type, "io.iec.edp.caf.sysmanager.api.manager.user.UserManager.getUsersByFieldIn", "Sys", params, null);
        if (!CollectionUtils.isEmpty(userList)) {
            userList.forEach(user -> {
                userId2UserMap.put(user.getId(), user);
            });
        }
        return userId2UserMap;
    }

    /**
     * 根据用户code批量查询用户信息
     *
     * @param codeList
     * @return
     */
    public static Map<String, User> getUsersByCodes(List<String> codeList) {
        Map<String, User> userCode2UserMap = Maps.newHashMap();
        RpcClient client = SpringBeanUtils.getBean(RpcClient.class);
        LinkedHashMap<String, Object> params = new LinkedHashMap<>();
        params.put("list", codeList);
        params.put("field", "code");
        params.put("mode", null);
        Type<List> type = new Type<>(List.class, User.class);
        List<User> userList = client.invoke(type, "io.iec.edp.caf.sysmanager.api.manager.user.UserManager.getUsersByFieldIn", "Sys", params, null);
        if (!CollectionUtils.isEmpty(userList)) {
            userList.forEach(user -> {
                userCode2UserMap.put(user.getCode(), user);
            });
        }
        return userCode2UserMap;
    }
}
